﻿Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        GetExternalIP()
        GetInternalIP()
        GetNetworkStatus()
        GetTotalNumberOfServers()
        InitializeServers()
    End Sub

    Private Function InitializeServers()
        Dim serverspath = Application.StartupPath & "\servers\"
        If IO.Directory.Exists(serverspath) Then
            LoadServers()
        Else
            MsgBox("Servers folder path does not exist.", MsgBoxStyle.Critical, )
            Me.Close()
        End If
        Log("Servers have been initialised.")
        Dim b = ""
        Return b
    End Function

    Private Function Log(ByVal text As String)
        If TextBox1.Text.Length > 0 Then
            TextBox1.Text = TextBox1.Text & vbNewLine & DateTime.Now & ">> " & text
        Else
            TextBox1.Text = TextBox1.Text & DateTime.Now & ">> " & text
        End If
        Dim b = ""
        Return b
    End Function

    Private Function ReloadServerInfo()
        serverip.Text = IO.File.ReadAllText(Application.StartupPath & "/servers/" & ListBox1.SelectedItem & ".txt")
        Log("Server information reloaded.")
        Dim b = ""
        Return b
    End Function

    Private Function GetTotalNumberOfServers()
        Dim number = 0
        Dim serverspath = Application.StartupPath & "\servers\"
        For Each file As String In My.Computer.FileSystem.GetFiles(serverspath, FileIO.SearchOption.SearchTopLevelOnly, "*.txt")
            number = number + 1
        Next
        Log("Total servers: " & number)
        Dim b = ""
        Return b
    End Function

    Private Function LoadServers()
        ListBox1.Items.Clear()
        Dim serverspath = Application.StartupPath & "\servers\"
        For Each file As String In My.Computer.FileSystem.GetFiles(serverspath, FileIO.SearchOption.SearchTopLevelOnly, "*.txt")
            file = file.Replace(".txt", "")
            file = file.Replace(Application.StartupPath & "\servers\", "")
            ListBox1.Items.Add(file)
        Next
        serverip.Clear()
        gameserver.ForeColor = Color.Black
        loginserver.ForeColor = Color.Black
        loginserver.Text = "N.A."
        gameserver.Text = "N.A."
        Log("Servers loaded.")
        Dim b = ""
        Return b
    End Function

    Private Function GetNetworkStatus()
        If ipaddress.Text.Length > 6 Then
            netstatus.Text = "Online"
            netstatus.ForeColor = Color.Green
        Else
            netstatus.Text = "Offline"
            netstatus.ForeColor = Color.Red
        End If
        Log("Network status aquired.")
        Dim b = ""
        Return b
    End Function

    Private Function GetInternalIP()
        Dim LocalHost As String
        LocalHost = System.Net.Dns.GetHostName()
        Dim ipEnter As System.Net.IPHostEntry = System.Net.Dns.GetHostByName(LocalHost)
        internalip.Text = ipEnter.AddressList(0).ToString
        Log("Internal IP address aquired.")
        Dim b = ""
        Return b
    End Function

    Private Function GetExternalIP()
        Try
            Dim WC As New System.Net.WebClient
            ipaddress.Text = System.Text.Encoding.ASCII.GetString((WC.DownloadData("http://whatismyip.com/automation/n09230945.asp")))
            WC.Dispose()
            Log("External IP address aquired.")
        Catch ex As Exception
            MsgBox(ex.Message)
            Log("External IP error: " & ex.Message)
        End Try
        Dim b = ""
        Return b
    End Function

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        Process.Start(LinkLabel1.Text)
        Log("Transferring you to project homepage.")
    End Sub

    Private Sub LinkLabel2_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked
        Process.Start("http://l2connector.update.freehostia.com/L2.NET%20Connector%20Documentation.zip")
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        serverip.Text = IO.File.ReadAllText(Application.StartupPath & "/servers/" & ListBox1.SelectedItem & ".txt")
    End Sub
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        TextBox1.HideSelection = False
        TextBox1.SelectionLength = 0
        TextBox1.SelectionStart = Len(TextBox1.Text)
        TextBox1.ScrollToCaret()
    End Sub

    Function WriteHosts()
        If serverip.Text.Length > 0 Then
            Try
                If IO.File.Exists("C:\Windows\system32\drivers\etc\hosts") Then
                    IO.File.Delete("C:\Windows\system32\drivers\etc\hosts")
                    Dim i As IO.StreamWriter
                    i = IO.File.AppendText("C:\Windows\System32\drivers\etc\hosts")
                    i.WriteLine("# Copyright (c) 1993-2009 Microsoft Corp.")
                    i.WriteLine("# Edit by L2.NET Connector v2 - l2connector.codeplex.com")
                    i.WriteLine("127.0.0.1  localhost")
                    i.WriteLine(serverip.Text & " L2authd.lineage2.com")
                    i.WriteLine("216.107.250.194 nprotect.lineage2.com")
                    i.WriteLine("216.107.250.194 update.nProtect.com")
                    i.WriteLine("216.107.250.194 update.nProtect.net")
                    i.Close()
                Else
                    Dim i As IO.StreamWriter
                    i = IO.File.AppendText("C:\Windows\system32\drivers\etc\hosts")
                    i.WriteLine("# Copyright (c) 1993-2009 Microsoft Corp.")
                    i.WriteLine("# Edit by L2.NET Connector v2 - l2connector.codeplex.com")
                    i.WriteLine("127.0.0.1  localhost")
                    i.WriteLine(serverip.Text & " L2authd.lineage2.com")
                    i.WriteLine("216.107.250.194 nprotect.lineage2.com")
                    i.WriteLine("216.107.250.194 update.nProtect.com")
                    i.WriteLine("216.107.250.194 update.nProtect.net")
                    i.Close()
                End If
                Log("Hosts file has been updated!")
            Catch ex As Exception
                MsgBox(ex.Message)
                Log("Hosts file cannot be updated.")
                Log(ex.Message)
            End Try
        Else
            MsgBox("Please choose your server before updating hosts.")
        End If
    End Function

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        WriteHosts()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        BugReport.Show()
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Me.Close()
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MsgBox("Coming soon...")
    End Sub

    Private Sub SaveLogToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveLogToolStripMenuItem.Click
        Dim o As SaveFileDialog = New SaveFileDialog
        o.Filter = "Text Files(*.txt)|*.txt"
        If o.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim writer As IO.StreamWriter
            writer = IO.File.AppendText(o.FileName)
            writer.WriteLine(TextBox1.Text)
            writer.Close()
        End If
    End Sub

    Private Sub CopyLogToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyLogToolStripMenuItem.Click
        Try
            Clipboard.SetText(TextBox1.Text)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub ClearLogToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearLogToolStripMenuItem.Click
        TextBox1.Clear()
    End Sub

    Private Function CheckStatus()
        Me.Enabled = False
        gameserver.Text = "N.A."
        loginserver.Text = "N.A."
        gameserver.ForeColor = Color.Black
        loginserver.ForeColor = Color.Black
        Try
            If serverip.Text.Length > 0 Then
                If Label6.Text = "Online" And Label7.Text = "Online" Then
                Else
                    Dim k As New System.Net.Sockets.TcpClient
                    k.NoDelay = True
                    k.Connect(serverip.Text, 2106)
                    If k.Connected = True Then
                        loginserver.Text = "Online"
                        loginserver.ForeColor = Color.Green
                    Else
                        loginserver.Text = "Offline"
                        loginserver.ForeColor = Color.Red
                    End If

                    Dim o As New System.Net.Sockets.TcpClient
                    o.NoDelay = True
                    o.Connect(serverip.Text, 9014)
                    If o.Connected = True Then
                        loginserver.Text = "Online"
                        loginserver.ForeColor = Color.Green
                    Else
                        loginserver.Text = "Offline"
                        loginserver.ForeColor = Color.Red
                    End If

                    Dim j As New System.Net.Sockets.TcpClient
                    j.NoDelay = True
                    j.Connect(serverip.Text, 7777)
                    If j.Connected = True Then
                        gameserver.Text = "Online"
                        gameserver.ForeColor = Color.Green
                    Else
                        gameserver.Text = "Offline"
                        gameserver.ForeColor = Color.Red
                    End If

                    Dim f As New System.Net.Sockets.TcpClient
                    f.NoDelay = True
                    f.Connect(serverip.Text, 7778)
                    If j.Connected = True Then
                        gameserver.Text = "Online"
                        gameserver.ForeColor = Color.Green
                    Else
                        gameserver.Text = "Offline"
                        gameserver.ForeColor = Color.Red
                    End If
                End If
            Else
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Me.Enabled = True
    End Function

    Private Sub Button5_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Process.Start("http://l2connector.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=33228")
    End Sub

    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        LoadServers()
    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If ListBox1.SelectedItem.ToString.Length > 0 Then
            ReloadServerInfo()
        Else
            MsgBox("No server selected.")
        End If
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        If ListBox1.SelectedItem.ToString.Length > 0 Then
            MsgBox("Please be patient and wait as this may take a few seconds.")
            Try
                CheckStatus()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        Else
            MsgBox("No server selected.")
        End If
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        Process.Start("http://l2connector.codeplex.com")
    End Sub
End Class
